package cn.zxone.feishu.demo.common.pojo;

import cn.zxone.feishu.demo.common.enums.ResultCode;
import lombok.Data;

import java.io.Serializable;


/**
 * 统一响应消息报文
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码，0==成功
     */
    private Integer code;

    /**
     * 消息内容
     */
    private String message;

    /**
     * 时间戳
     */
    private long timestamp;

    /**
     * 业务数据
     */
    private T data;


    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success(T data) {
        return restResult(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return restResult(ResultCode.SUCCESS, null);
    }

    public static <T> Result<T> failed(int code, String message) {
        return restResult(code, message, null);
    }

    public static <T> Result<T> failed(String message, T data) {
        return restResult(ResultCode.FAILED.getCode(), message, data);
    }

    public static <T> Result<T> failed(int code, String message, T data) {
        return restResult(code, message, data);
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return restResult(resultCode, null);
    }

    public static <T> Result<T> failed(IResultCode resultCode, String message) {
        return restResult(resultCode.getCode(), message, null);
    }

    public static <T> Result<T> failed(String message) {

        return restResult(ResultCode.FAILED.getCode(), message, null);
    }


    public static <T> Result<T> restResult(IResultCode resultCode, T data) {
        return restResult(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public static <T> Result<T> restResult(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> restResult(IResultCode resultCode, String message, T data) {

        return restResult(resultCode.getCode(), message, data);
    }


}
